<template>
  <div class="container">
    <div class="login-card">
      <div class="card-header fade-in">
        <div class="logo pulse">
          <i class="fa fa-feather" aria-hidden="true"></i>
        </div>
        <h1 class="card-title">博客空间</h1>
        <p class="card-subtitle">记录生活的点滴，分享思考的火花</p>
      </div>
      
      <div class="card-body">
        <form @submit="handleSubmit">
          <div class="form-group slide-up delay-1">
            <label for="username" class="form-label">用户名或邮箱</label>
            <input type="text" id="username" class="form-input" placeholder="请输入用户名或邮箱" v-model="loginForm.username">
            <span class="form-icon"><i class="ri-user-line"></i></span>
          </div>
          
          <div class="form-group slide-up delay-2">
            <label for="password" class="form-label">密码</label>
            <input id="password" class="form-input" placeholder="请输入密码" v-model="loginForm.password" :type="passwordVisible ? 'text' : 'password'">
            <span 
              class="form-icon password-toggle"
              aria-hidden="true"
              @click="togglePasswordVisibility">
              <i :class="passwordVisible ? 'ri-eye-line' : 'ri-eye-off-line'"></i>
            </span>
          </div>
          
          <div class="form-remember slide-up delay-3">
            <div class="remember-me">
              <input type="checkbox" id="remember">
              <label for="remember">记住我</label>
            </div>
            <a href="#" class="forgot-password">忘记密码？</a>
          </div>
          
          <button type="submit" class="form-button slide-up delay-3" :disabled="isLoading">
            <template v-if="!isLoading">
              <i class="fa fa-sign-in" aria-hidden="true"></i>
              登录
            </template>
            <template v-else>
              <i class="fa fa-spinner fa-spin" aria-hidden="true"></i> 
              登录中...
            </template>
          </button>
          
          <div class="divider slide-up delay-3">
            <span class="divider-text">或者使用</span>
          </div>
          
          <div class="social-buttons slide-up delay-3">
            <a href="#" class="social-button">
              <i class="ri-wechat-fill"></i>
            </a>
            <a href="#" class="social-button">
              <i class="ri-github-fill"></i>
            </a>
            <a href="#" class="social-button">
              <i class="ri-google-fill"></i>
            </a>
          </div>
        </form>
      </div>
      
      <div class="card-footer fade-in">
        <p class="footer-text">
          还没有账号？<a href="#">立即注册</a>
        </p>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue';
import { useRouter } from 'vue-router';
import useLoginState from '@/stores/userinfo';
import { ElMessage } from 'element-plus';

const router = useRouter();
const loginState = useLoginState();
const loginForm = ref({
  username: 'admin',
  password: ''
});
const passwordVisible = ref(false);
const isLoading = ref(false);

const handleSubmit = async function(e) {
  e.preventDefault();

  isLoading.value = true;
  try {
    const resp = await loginState.doLogin({
      username: loginForm.value.username,
      password: loginForm.value.password
    });
    if (resp.code === 200) {
      router.push('/');
    } else {
      ElMessage.error(resp.msg);
    }
  } catch (error) {
    console.error('登录失败', error);
    ElMessage.error('登录失败，请稍后重试');
  }
  isLoading.value = false;
}

const togglePasswordVisibility = function() {
  passwordVisible.value = !passwordVisible.value;
}
</script>

<style scoped>
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
  background-color: #FAFAFA;
  color: #333333; 
  line-height: 1.6;
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 20px;
  position: relative;
  overflow-x: hidden;
}

/* 背景装饰 */
body::before {
  content: "";
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-image: url('@/assets/background.jpg');
  background-size: cover;
  background-position: center;
  opacity: 0.12;
  filter: grayscale(20%) blur(1px);
  z-index: 0;
}

body::after {
  background: linear-gradient(to bottom right, rgba(255,255,255,0.9), rgba(255,255,255,0.6));
  z-index: 1;
}

.container {
  max-width: 400px;
  width: 100%;
  z-index: 2;
  position: relative;
}

/* 登录卡片 */
.login-card {
  background-color: #FFFFFF;
  border-radius: 24px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
  overflow: hidden;
  transition: transform 0.3s ease, box-shadow 0.3s ease;
  animation: fadeInUp 0.5s ease forwards;
  opacity: 0;
  transform: translateY(20px);
}

.login-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 15px 35px rgba(0, 0, 0, 0.15);
}

@keyframes fadeInUp {
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.card-header {
  padding: 30px 40px 20px;
  text-align: center;
  position: relative;
}

.card-header::after {
  content: "";
  position: absolute;
  bottom: 0;
  left: 40px;
  right: 40px;
  height: 1px;
  background: linear-gradient(to right, transparent, var(#4CAF50), transparent);
}

.logo {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 80px;
  height: 80px;
  background-color: #E8F5E9;
  border-radius: 50%;
  margin-bottom: 20px;
  transition: transform 0.3s ease;
}

.logo:hover {
  transform: rotate(10deg) scale(1.05);
}

.logo i {
  font-size: 32px;
  color: #4CAF50;
}

.card-title {
  font-size: 28px;
  font-weight: 700;
  color: #333333;
  margin-bottom: 10px;
}

.card-subtitle {
  font-size: 16px;
  color: #667085;
  max-width: 280px;
  margin: 0 auto;
}

.card-body {
  padding: 30px 40px;
}

.form-group {
  margin-bottom: 25px;
  position: relative;
}

.form-label {
  display: block;
  margin-bottom: 8px;
  font-size: 14px;
  color: #667085;
  font-weight: 500;
}

.form-input {
  width: 100%;
  padding: 15px 45px 15px 15px;
  border: 1px solid #E0E0E0;
  border-radius: 12px;
  font-size: 16px;
  color: #333333;
  background-color: transparent;
  transition: border-color 0.3s ease, box-shadow 0.3s ease;
}

.form-input:focus {
  outline: none;
  border-color: #4CAF50;
  box-shadow: 0 0 0 3px rgba(76, 175, 80, 0.15);
}

.form-input::placeholder {
  color: #BDBDBD;
}

.form-icon {
  position: absolute;
  right: 15px;
  top: 70%;
  transform: translateY(-50%);
  color: #667085;
  font-size: 20px;
  transition: color 0.3s ease;
  left: 88%;
}

.form-input:focus + .form-icon {
  color: #4CAF50;
}

.form-remember {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 30px;
}

.remember-me {
  display: flex;
  align-items: center;
}

.remember-me input {
  margin-right: 8px;
}

.remember-me label {
  font-size: 14px;
  color: #667085;
}

.forgot-password {
  font-size: 14px;
  color: #4CAF50;
  text-decoration: none;
  transition: color 0.3s ease;
}

.forgot-password:hover {
  color: #388E3C;
}

.form-button {
  width: 100%;
  padding: 15px;
  background-color: #4CAF50;
  color: white;
  border: none;
  border-radius: 12px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: background-color 0.3s ease, transform 0.3s ease;
  display: flex;
  align-items: center;
  justify-content: center;
}

.form-button:hover {
  background-color: #388E3C;
  transform: translateY(-2px);
}

.form-button:active {
  transform: translateY(0);
}

.form-button i {
  margin-right: 8px;
}

.divider {
  display: flex;
  align-items: center;
  margin: 30px 0;
}

.divider::before,
.divider::after {
  content: "";
  flex-grow: 1;
  height: 1px;
  background-color: #E0E0E0;
}

.divider-text {
  margin: 0 15px;
  font-size: 14px;
  color: #667085;
}

.social-buttons {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 15px;
}

.social-button {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 12px;
  border: 1px solid #E0E0E0;
  border-radius: 12px;
  color: #667085;
  text-decoration: none;
  transition: border-color 0.3s ease, background-color 0.3s ease;
}

.social-button:hover {
  border-color: #4CAF50;
  background-color: #E8F5E9;
  color: #4CAF50;
}

.social-button i {
  font-size: 20px;
}

.card-footer {
  padding: 20px 40px 30px;
  text-align: center;
}

.footer-text {
  font-size: 14px;
  color: #667085;
}

.footer-text a {
  color: #4CAF50;
  text-decoration: none;
  transition: color 0.3s ease;
}

.footer-text a:hover {
  color: #388E3C;
}

/* 响应式设计 */
@media (max-width: 420px) {
  .card-header,
  .card-body,
  .card-footer {
    padding: 25px 25px;
  }
  
  .social-buttons {
    gap: 10px;
  }
}

/* 动画效果 */
.fade-in {
  animation: fadeIn 0.5s ease forwards;
  opacity: 0;
}

@keyframes fadeIn {
  to {
    opacity: 1;
  }
}

.slide-up {
  animation: slideUp 0.5s ease forwards;
  opacity: 0;
  transform: translateY(10px);
}

@keyframes slideUp {
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.delay-1 {
  animation-delay: 0.1s;
}

.delay-2 {
  animation-delay: 0.2s;
}

.delay-3 {
  animation-delay: 0.3s;
}

.pulse {
  animation: pulse 2s infinite;
}

@keyframes pulse {
  0% {
    transform: scale(1);
  }
  50% {
    transform: scale(1.05);
  }
  100% {
    transform: scale(1);
  }
}

.password-toggle {
  right: 35px;
  cursor: pointer;
  color: #667085;
  transition: color 0.3s;
}

.password-toggle:hover {
  color: #4CAF50;
}

.form-input[type="password"],
.form-input[type="text"] {
  padding-right: 65px;
}

</style>